package com.ruoyi.cms.websocket;

import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.actuate.endpoint.web.annotation.ServletEndpoint;
import org.springframework.stereotype.Component;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 杜晓南
 * @version 1.0
 * @description: TODO
 * @date 2024/7/19 15:59
 */
@ServerEndpoint("/chat/{room}/{username}")
@Component
public class ChatChantel {
//    private static List<Session> userList=new Vector<>();

    private static Map<String,List<Session>> userMap =new ConcurrentHashMap<>();

    @OnOpen
    public void onOpen(Session session,@PathParam("room")String room, @PathParam("username")String username) throws IOException {
//        userList.add(session);

        List<Session> userList = userMap.get(room);
        if(userList==null){
            userList = new Vector<>();
            userMap.put(room,userList);
        }
        userList.add(session);


        System.out.println("open-----"+username+"进入直播间------当前在线用户"+userList.size());

        Map<String, Object> userProperties = session.getUserProperties();
        userProperties.put("username",username);
        userProperties.put("room",room);

        session.getBasicRemote().sendText("当前直播在线人数"+userList.size()+"人");
    }
    @OnMessage
    public void onMessage(String message,Session session) throws IOException {
        System.out.println("message-----"+message);

        String username = (String) session.getUserProperties().get("username");
        String room = (String) session.getUserProperties().get("room");

        Map map= JSONObject.parseObject(message, Map.class);
        String type = (String) map.get("type");
        String username1 = (String) map.get("username");
        String content =(String) map.get("content");

        if(type.equals("1")){
            List<Session> sessions = userMap.get(room);
            for (Session user : sessions) {
                user.getBasicRemote().sendText(username+":"+content);
            }
        }else{
            List<Session> sessions = userMap.get(room);
            for (Session user : sessions) {
                String username2 = (String) user.getUserProperties().get("username");
                if(username2.equals(username1)){
                    user.getBasicRemote().sendText("[私]"+username+":"+content);
                }
            }
            session.getBasicRemote().sendText("[私]"+username+":"+content);
        }
    }
    @OnClose
    public void onClose(Session session){
        String room = (String) session.getUserProperties().get("room");
        List<Session> sessions = userMap.get(room);

        sessions.remove(session);
        System.out.println("close-----当前在线用户"+sessions.size());
    }
    @OnError
    public void onError(Throwable throwable,Session session){
        System.out.println("error-----");
    }
}
